/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CarroDialog.java
 *
 * Created on 22/11/2011, 16:40:28
 */
package br.com.seguradora.view.dialog;

import br.com.seguradora.exception.InputValidationException;
import br.com.seguradora.exception.PersistenciaException;
import br.com.seguradora.model.Associado;
import br.com.seguradora.model.CalculoCotas;
import br.com.seguradora.model.Grupo;
import br.com.seguradora.model.Marca;
import br.com.seguradora.model.Modelo;
import br.com.seguradora.model.Veiculo;
import br.com.seguradora.model.constant.EspecieVeiculo;
import br.com.seguradora.model.constant.TipoCombustivel;
import br.com.seguradora.model.constant.TipoVeiculo;
import br.com.seguradora.service.VeiculoService;
import br.com.seguradora.view.ErrorsList;
import br.com.seguradora.view.StateEntityWindow;
import br.com.seguradora.view.UserError;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import um.filteredtext.number.CurrencyNumberAdapter;

/**
 *
 * @author Diego
 */
public class VeiculoDialog extends javax.swing.JDialog {

    private Veiculo veiculo;
    private final static String titleWindow = "Veículo";
    private StateEntityWindow stateEntity;
    private CurrencyNumberAdapter valorFormat;
    private CurrencyNumberAdapter mensalidadeFormat; 
    private CurrencyNumberAdapter franquiaFormat;
    private DecimalFormat df = new DecimalFormat("0.00");
    

    public VeiculoDialog(java.awt.Frame parent, boolean modal, StateEntityWindow stateEntity, Veiculo veiculo) {
        super(parent, modal);
        initComponents();
        this.stateEntity = stateEntity;
        setTitle(titleWindow + stateEntity);
        organizeWindow();
        setVeiculo(veiculo);
        setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cabecalhoPanel = new br.com.seguradora.view.panel.CabecalhoPanel();
        proprietarioErrorLabel = new javax.swing.JLabel();
        proprietarioField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        renavamField = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        exercicioField = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        cpfField = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        placaField = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        chassiField = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tipoCombo = new javax.swing.JComboBox();
        especieCombo = new javax.swing.JComboBox();
        combustivelCombo = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        codigoModeloField = new javax.swing.JTextField();
        nomeModeloField = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        codigoMarcaField = new javax.swing.JTextField();
        nomeMarcaField = new javax.swing.JTextField();
        exercicioErrorLabel = new javax.swing.JLabel();
        renavamErrorLabel = new javax.swing.JLabel();
        cpfErrorLabel = new javax.swing.JLabel();
        placaErrorLabel = new javax.swing.JLabel();
        chassiErrorLabel = new javax.swing.JLabel();
        modeloErrorLabel = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        anoFabricacaoField = new javax.swing.JFormattedTextField();
        jLabel13 = new javax.swing.JLabel();
        anoModeloField = new javax.swing.JFormattedTextField();
        anoFabricacaoErrorLabel = new javax.swing.JLabel();
        anoModeloErrorLabel = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        especificacaoField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        categoriaField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        corField = new javax.swing.JTextField();
        categoriaErrorLabel = new javax.swing.JLabel();
        corErrorLabel = new javax.swing.JLabel();
        abrirModelosButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        codigoAssociadoField = new javax.swing.JTextField();
        nomeAssociadoField = new javax.swing.JTextField();
        abrirAssociadosButton = new javax.swing.JButton();
        associadoErrorLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        codigoGrupoField = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        grupoErrorLabel = new javax.swing.JLabel();
        abrirGruposButton = new javax.swing.JButton();
        nomeGrupoField = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        valorField = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        cotasField = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        mensalidadeField = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        franquiaField = new javax.swing.JTextField();
        valorErrorLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel23 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        observacaoField = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        confirmarButton = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(579, 480));

        jLabel1.setText("Proprietário:");

        proprietarioErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        jLabel2.setText("Renavam:");

        try {
            renavamField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#########")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        renavamField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel7.setText("Exercício:");

        try {
            exercicioField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        exercicioField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel3.setText("CPF:");

        try {
            cpfField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cpfField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel4.setText("Placa:");

        try {
            placaField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("UUU-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        placaField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel5.setText("Chassi:");

        try {
            chassiField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("AAAAAAAAAAAAAAAAA")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        chassiField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel6.setText("Tipo:");

        jLabel8.setText("Espécie:");

        jLabel9.setText("Combustível:");

        tipoCombo.setModel(new DefaultComboBoxModel(TipoVeiculo.values()));

        especieCombo.setModel(new DefaultComboBoxModel(EspecieVeiculo.values()));

        combustivelCombo.setModel(new DefaultComboBoxModel(TipoCombustivel.values()));

        jLabel10.setText("Modelo:");

        codigoModeloField.setEditable(false);
        codigoModeloField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoModeloField.setFocusable(false);

        nomeModeloField.setEditable(false);
        nomeModeloField.setFocusable(false);

        jLabel11.setText("Marca:");

        codigoMarcaField.setEditable(false);
        codigoMarcaField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoMarcaField.setFocusable(false);

        nomeMarcaField.setEditable(false);
        nomeMarcaField.setFocusable(false);

        exercicioErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        renavamErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        cpfErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        placaErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        chassiErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        modeloErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        jLabel12.setText("Ano Fabricação:");

        try {
            anoFabricacaoField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        anoFabricacaoField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jLabel13.setText("Ano Modelo:");

        try {
            anoModeloField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        anoModeloField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        anoFabricacaoErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        anoModeloErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        jLabel14.setText("CAP / POT / CIL:");

        jLabel15.setText("Categoria:");

        jLabel16.setText("Cor:");

        categoriaErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        corErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        abrirModelosButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/magnifier.png"))); // NOI18N
        abrirModelosButton.setToolTipText("Abrir pesquisa de modelos");
        abrirModelosButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        abrirModelosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirModelosButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cabecalhoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel14)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addGap(17, 17, 17)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel7)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel8)
                                        .addComponent(jLabel9)
                                        .addComponent(jLabel10)
                                        .addComponent(jLabel11)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel13)))
                                .addComponent(jLabel15)
                                .addComponent(jLabel16)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(corField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(corErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(categoriaField, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(categoriaErrorLabel))
                            .addComponent(especificacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(anoModeloField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(anoModeloErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(codigoMarcaField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nomeMarcaField, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(combustivelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(chassiField, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(chassiErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(placaField, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(placaErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(exercicioField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(exercicioErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(renavamField, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(renavamErrorLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cpfField, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cpfErrorLabel))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(especieCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tipoCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, 120, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(codigoModeloField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(nomeModeloField)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(abrirModelosButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(proprietarioField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(4, 4, 4)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(modeloErrorLabel)
                                    .addComponent(proprietarioErrorLabel)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(anoFabricacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(anoFabricacaoErrorLabel)))))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(cabecalhoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(renavamField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(exercicioField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(exercicioErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(renavamErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(proprietarioField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(proprietarioErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(cpfField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cpfErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(placaErrorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(placaField)
                                .addComponent(jLabel4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(chassiField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel6)
                                    .addComponent(tipoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(especieCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(combustivelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(chassiErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(codigoModeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nomeModeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(modeloErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(abrirModelosButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(codigoMarcaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nomeMarcaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel12)
                        .addComponent(anoFabricacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(anoFabricacaoErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel13)
                        .addComponent(anoModeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(anoModeloErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(especificacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(categoriaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(categoriaErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel16)
                        .addComponent(corField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(corErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(93, 93, 93))
        );

        tabbedPane.addTab("Identificação", jPanel1);

        jLabel17.setText("Associado:");

        codigoAssociadoField.setEditable(false);
        codigoAssociadoField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoAssociadoField.setFocusable(false);

        nomeAssociadoField.setEditable(false);
        nomeAssociadoField.setFocusable(false);

        abrirAssociadosButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/magnifier.png"))); // NOI18N
        abrirAssociadosButton.setToolTipText("Abrir pesquisa de associados");
        abrirAssociadosButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        abrirAssociadosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirAssociadosButtonActionPerformed(evt);
            }
        });

        associadoErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        codigoGrupoField.setEditable(false);
        codigoGrupoField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        codigoGrupoField.setFocusable(false);

        jLabel18.setText("Grupo:");

        grupoErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        abrirGruposButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/magnifier.png"))); // NOI18N
        abrirGruposButton.setToolTipText("Abrir pesquisa de grupos");
        abrirGruposButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        abrirGruposButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirGruposButtonActionPerformed(evt);
            }
        });

        nomeGrupoField.setEditable(false);
        nomeGrupoField.setFocusable(false);

        jLabel19.setText("Valor:");

        valorField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorFieldActionPerformed(evt);
            }
        });
        valorField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                valorFieldFocusLost(evt);
            }
        });

        jLabel20.setText("Cotas:");

        cotasField.setEditable(false);
        cotasField.setFocusable(false);

        jLabel21.setText("Mensalidade:");

        mensalidadeField.setEditable(false);
        mensalidadeField.setFocusable(false);

        jLabel22.setText("Franquia:");

        franquiaField.setEditable(false);
        franquiaField.setFocusable(false);

        valorErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/exclamation.png"))); // NOI18N

        jLabel23.setText("Observação:");

        observacaoField.setColumns(20);
        observacaoField.setLineWrap(true);
        observacaoField.setRows(5);
        observacaoField.setWrapStyleWord(true);
        jScrollPane1.setViewportView(observacaoField);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel17)
                    .addComponent(jLabel18))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(codigoAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nomeAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(abrirAssociadosButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(associadoErrorLabel))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(codigoGrupoField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nomeGrupoField, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(abrirGruposButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(grupoErrorLabel)))
                .addGap(39, 39, 39))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20)
                    .addComponent(jLabel21)
                    .addComponent(jLabel22))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(valorField, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(valorErrorLabel))
                    .addComponent(cotasField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mensalidadeField, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(franquiaField, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(335, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel23)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(nomeAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(associadoErrorLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(abrirAssociadosButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel17)
                        .addComponent(codigoAssociadoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(codigoGrupoField)
                        .addComponent(jLabel18))
                    .addComponent(nomeGrupoField)
                    .addComponent(abrirGruposButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(grupoErrorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(valorErrorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(valorField)
                        .addComponent(jLabel19)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(cotasField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(mensalidadeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(franquiaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel23)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                .addContainerGap())
        );

        tabbedPane.addTab("Associado, Grupo e Valores", jPanel2);

        getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);

        confirmarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/accept.png"))); // NOI18N
        confirmarButton.setText("Confirmar");
        confirmarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmarButtonActionPerformed(evt);
            }
        });

        cancelarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/cross.png"))); // NOI18N
        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addComponent(confirmarButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelarButton)
                .addContainerGap(269, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmarButton)
                    .addComponent(cancelarButton))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmarButtonActionPerformed
        try {
            validateUserInput();
            VeiculoService veiculoService = new VeiculoService();
            Veiculo veiculo = getVeiculo();
            if (getStateEntity() == StateEntityWindow.NEW) {
                veiculoService.inserir(veiculo);
            } else if (getStateEntity() == StateEntityWindow.UPDATE) {
                veiculoService.atualizar(veiculo);
            }
            dispose();
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        } catch (InputValidationException ivex) {
            JOptionPane.showMessageDialog(null, "Erros foram encontrados.", "Mensagem do sistema", JOptionPane.ERROR_MESSAGE);
            ErrorsList errorsList = ivex.getErrorsList();
            for (Iterator<UserError> it = errorsList.iterator(); it.hasNext();) {
                UserError userError = it.next();
                showErrorLabel(userError);
            }
        }
}//GEN-LAST:event_confirmarButtonActionPerformed

    private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
        dispose();
}//GEN-LAST:event_cancelarButtonActionPerformed

    private void abrirModelosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirModelosButtonActionPerformed
        ConsultaModeloDialog dialog = new ConsultaModeloDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
        dialog.setSelectEntity(true);
        dialog.setVisible(true);
        Modelo modelo = dialog.getModelo();
        setModelo(modelo);
}//GEN-LAST:event_abrirModelosButtonActionPerformed

    private void abrirAssociadosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirAssociadosButtonActionPerformed
        ConsultaAssociadoDialog dialog = new ConsultaAssociadoDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
        dialog.setSelectEntity(true);
        dialog.setVisible(true);
        Associado associado = dialog.getAssociado();
        setAssociado(associado);
}//GEN-LAST:event_abrirAssociadosButtonActionPerformed

    private void abrirGruposButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirGruposButtonActionPerformed
        ConsultaGrupoDialog dialog = new ConsultaGrupoDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
        dialog.setSelectEntity(true);
        dialog.setVisible(true);
        Grupo grupo = dialog.getGrupo();
        setGrupo(grupo);
}//GEN-LAST:event_abrirGruposButtonActionPerformed

    private void valorFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorFieldActionPerformed
        calcularCotas();
    }//GEN-LAST:event_valorFieldActionPerformed

    private void valorFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_valorFieldFocusLost
        calcularCotas();
    }//GEN-LAST:event_valorFieldFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VeiculoDialog dialog = new VeiculoDialog(new javax.swing.JFrame(), true, StateEntityWindow.NEW, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abrirAssociadosButton;
    private javax.swing.JButton abrirGruposButton;
    private javax.swing.JButton abrirModelosButton;
    private javax.swing.JLabel anoFabricacaoErrorLabel;
    private javax.swing.JFormattedTextField anoFabricacaoField;
    private javax.swing.JLabel anoModeloErrorLabel;
    private javax.swing.JFormattedTextField anoModeloField;
    private javax.swing.JLabel associadoErrorLabel;
    private br.com.seguradora.view.panel.CabecalhoPanel cabecalhoPanel;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JLabel categoriaErrorLabel;
    private javax.swing.JTextField categoriaField;
    private javax.swing.JLabel chassiErrorLabel;
    private javax.swing.JFormattedTextField chassiField;
    private javax.swing.JTextField codigoAssociadoField;
    private javax.swing.JTextField codigoGrupoField;
    private javax.swing.JTextField codigoMarcaField;
    private javax.swing.JTextField codigoModeloField;
    private javax.swing.JComboBox combustivelCombo;
    private javax.swing.JButton confirmarButton;
    private javax.swing.JLabel corErrorLabel;
    private javax.swing.JTextField corField;
    private javax.swing.JTextField cotasField;
    private javax.swing.JLabel cpfErrorLabel;
    private javax.swing.JFormattedTextField cpfField;
    private javax.swing.JComboBox especieCombo;
    private javax.swing.JTextField especificacaoField;
    private javax.swing.JLabel exercicioErrorLabel;
    private javax.swing.JFormattedTextField exercicioField;
    private javax.swing.JTextField franquiaField;
    private javax.swing.JLabel grupoErrorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField mensalidadeField;
    private javax.swing.JLabel modeloErrorLabel;
    private javax.swing.JTextField nomeAssociadoField;
    private javax.swing.JTextField nomeGrupoField;
    private javax.swing.JTextField nomeMarcaField;
    private javax.swing.JTextField nomeModeloField;
    private javax.swing.JTextArea observacaoField;
    private javax.swing.JLabel placaErrorLabel;
    private javax.swing.JFormattedTextField placaField;
    private javax.swing.JLabel proprietarioErrorLabel;
    private javax.swing.JTextField proprietarioField;
    private javax.swing.JLabel renavamErrorLabel;
    private javax.swing.JFormattedTextField renavamField;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JComboBox tipoCombo;
    private javax.swing.JLabel valorErrorLabel;
    private javax.swing.JTextField valorField;
    // End of variables declaration//GEN-END:variables

    private void setVeiculo(Veiculo veiculo) {
        if (veiculo == null) {
            return;
        }
        this.veiculo = veiculo;
        cabecalhoPanel.setCodigo(veiculo.getId());
        cabecalhoPanel.setBloqueado(veiculo.isBloqueado());
        renavamField.setText(veiculo.getRenavam());
        exercicioField.setText(veiculo.getExercicio());
        proprietarioField.setText(veiculo.getNomeProprietario());
        cpfField.setText(veiculo.getCpfProprietario());
        placaField.setText(veiculo.getPlaca());
        chassiField.setText(veiculo.getChassi());
        tipoCombo.setSelectedItem(veiculo.getTipo());
        especieCombo.setSelectedItem(veiculo.getEspecie());
        combustivelCombo.setSelectedItem(veiculo.getCombustivel());
        codigoModeloField.setText(String.valueOf(veiculo.getModelo().getId()));
        nomeModeloField.setText(String.valueOf(veiculo.getModelo().getNome()));
        codigoMarcaField.setText(String.valueOf(veiculo.getModelo().getMarca().getId()));
        nomeMarcaField.setText(String.valueOf(veiculo.getModelo().getMarca().getNome()));
        anoFabricacaoField.setText(veiculo.getAnoFabricacao());
        anoModeloField.setText(veiculo.getAnoModelo());
        especificacaoField.setText(veiculo.getEspecificacao());
        categoriaField.setText(veiculo.getCategoria());
        corField.setText(veiculo.getCorPredominante());
        codigoAssociadoField.setText(String.valueOf(veiculo.getAssociado().getId()));
        nomeAssociadoField.setText(String.valueOf(veiculo.getAssociado().getNome()));
        codigoGrupoField.setText(String.valueOf(veiculo.getGrupo().getId()));
        nomeGrupoField.setText(String.valueOf(veiculo.getGrupo().getNome()));
        valorField.setText(valorFormat.getNumberFormat().formatNumber(veiculo.getValor()));
        cotasField.setText(df.format(veiculo.getCotas()));
        mensalidadeField.setText(mensalidadeFormat.getNumberFormat().formatNumber(veiculo.getMensalidade()));
        franquiaField.setText(franquiaFormat.getNumberFormat().formatNumber(veiculo.getFranquia()));
        observacaoField.setText(veiculo.getObservacao());
        observacaoField.setCaretPosition(0);
    }

    public Veiculo getVeiculo() {
        if (veiculo == null) {
            veiculo = new Veiculo();
        }
        veiculo.setBloqueado(cabecalhoPanel.isBloqueado());
        veiculo.setRenavam(renavamField.getText());
        veiculo.setExercicio(exercicioField.getText());
        veiculo.setNomeProprietario(proprietarioField.getText().toUpperCase());
        veiculo.setCpfProprietario(cpfField.getText());
        veiculo.setPlaca(placaField.getText());
        veiculo.setChassi(chassiField.getText().toUpperCase());
        veiculo.setTipo((TipoVeiculo) tipoCombo.getSelectedItem());
        veiculo.setEspecie((EspecieVeiculo) especieCombo.getSelectedItem());
        veiculo.setCombustivel((TipoCombustivel) combustivelCombo.getSelectedItem());
        Modelo modelo = new Modelo(Long.parseLong(codigoModeloField.getText()), nomeModeloField.getText());
        modelo.setMarca(new Marca(Long.parseLong(codigoMarcaField.getText()), nomeMarcaField.getText()));
        veiculo.setModelo(modelo);
        veiculo.setAnoFabricacao(anoFabricacaoField.getText());
        veiculo.setAnoModelo(anoModeloField.getText());
        veiculo.setEspecificacao(especificacaoField.getText().toUpperCase());
        veiculo.setCategoria(categoriaField.getText().toUpperCase());
        veiculo.setCorPredominante(corField.getText().toUpperCase());
        veiculo.setAssociado(new Associado(Long.parseLong(codigoAssociadoField.getText()), nomeAssociadoField.getText()));
        veiculo.setGrupo(new Grupo(Long.parseLong(codigoGrupoField.getText())));
        try {
            Number valor = valorFormat.getNumberFormat().parseNumber(valorField.getText());
            veiculo.setValor(Double.valueOf(valor.toString()));
            veiculo.setCotas(Double.valueOf(df.parse(cotasField.getText()).toString()));
            Number mensalidade = mensalidadeFormat.getNumberFormat().parseNumber(mensalidadeField.getText());
            veiculo.setMensalidade(Double.valueOf(mensalidade.toString()));
            Number franquia = franquiaFormat.getNumberFormat().parseNumber(franquiaField.getText());
            veiculo.setFranquia(Double.valueOf(franquia.toString()));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            veiculo.setValor(0d);
            veiculo.setCotas(0d);
            veiculo.setMensalidade(0d);
            veiculo.setMensalidade(0d);
        }
        veiculo.setObservacao(observacaoField.getText());
        return veiculo;
    }

    private void organizeWindow() {
        if (getStateEntity() == StateEntityWindow.NEW) {
            cabecalhoPanel.isEnabledComponentBloqueado(false);
        }
        configTextFieldsNumericos();
        isShowErrorsLabel(false);
        if (!renavamField.isFocusOwner()) {
            renavamField.requestFocus();
        }
    }

    private void isShowErrorsLabel(boolean show) {
        renavamErrorLabel.setVisible(show);
        exercicioErrorLabel.setVisible(show);
        proprietarioErrorLabel.setVisible(show);
        cpfErrorLabel.setVisible(show);
        placaErrorLabel.setVisible(show);
        chassiErrorLabel.setVisible(show);
        modeloErrorLabel.setVisible(show);
        anoFabricacaoErrorLabel.setVisible(show);
        anoModeloErrorLabel.setVisible(show);
        categoriaErrorLabel.setVisible(show);
        corErrorLabel.setVisible(show);
        associadoErrorLabel.setVisible(show);
        grupoErrorLabel.setVisible(show);
        valorErrorLabel.setVisible(show);
    }

    private void showErrorLabel(UserError userError) {
        userError.getLabel().setVisible(true);
    }

    public StateEntityWindow getStateEntity() {
        return stateEntity;
    }

    public Veiculo objetoVeiculo() {
        return veiculo;
    }
    
    private void validateUserInput() throws InputValidationException {
        isShowErrorsLabel(false);
        ErrorsList errorsList = new ErrorsList();
        if (renavamField.getText().equals("         ")) {
            errorsList.addError(new UserError(renavamErrorLabel, "O renavam não foi informado."));
        }
        if (proprietarioField.getText().isEmpty()) {
            errorsList.addError(new UserError(proprietarioErrorLabel, "O nome do proprietário não foi informado."));
        }
        if (cpfField.getText().equals("   .   .   -  ")) {
            errorsList.addError(new UserError(cpfErrorLabel, "O cpf do proprietário não foi informado."));
        }
        if (placaField.getText().equals("   -    ")) {
            errorsList.addError(new UserError(placaErrorLabel, "A placa não foi informada."));
        }
        if (chassiField.getText().equals("                 ")) {
            errorsList.addError(new UserError(chassiErrorLabel, "O chassi não foi informado."));
        }
        if (nomeModeloField.getText().isEmpty()) {
            errorsList.addError(new UserError(modeloErrorLabel, "O modelo não foi informado."));
        }
        if (anoFabricacaoField.getText().equals("    ")) {
            errorsList.addError(new UserError(anoFabricacaoErrorLabel, "O ano de fabricação não foi informado."));
        }
        if (anoModeloField.getText().equals("    ")) {
            errorsList.addError(new UserError(anoModeloErrorLabel, "O ano do modelo não foi informado."));
        }
        if (corField.getText().isEmpty()) {
            errorsList.addError(new UserError(corErrorLabel, "A cor não foi informada."));
        }
        if (nomeAssociadoField.getText().isEmpty()) {
            errorsList.addError(new UserError(associadoErrorLabel, "O associado não foi informado."));
        }
        if (nomeGrupoField.getText().isEmpty()) {
            errorsList.addError(new UserError(grupoErrorLabel, "O grupo não foi informado."));
        }
        if (valorField.getText().isEmpty()) {
            errorsList.addError(new UserError(valorErrorLabel, "O valor não foi informado."));
        }
        if (errorsList.countErrors() > 0) {
            throw new InputValidationException(errorsList);
        }
    }

    private void setModelo(Modelo modelo) {
        if (modelo != null) {
            codigoModeloField.setText(String.valueOf(modelo.getId()));
            nomeModeloField.setText(modelo.getNome());
            codigoMarcaField.setText(String.valueOf(modelo.getMarca().getId()));
            nomeMarcaField.setText(modelo.getMarca().getNome());
        }
    }

    private void setAssociado(Associado associado) {
        if (associado != null) {
            codigoAssociadoField.setText(String.valueOf(associado.getId()));
            nomeAssociadoField.setText(associado.getNome());
        }
    }

    private void setGrupo(Grupo grupo) {
        if (grupo != null) {
            codigoGrupoField.setText(String.valueOf(grupo.getId()));
            nomeGrupoField.setText(grupo.getNome());
        }
    }

    private void configTextFieldsNumericos() {
        valorFormat = new CurrencyNumberAdapter(false);
        valorField.addKeyListener(valorFormat);
        valorField.addFocusListener(valorFormat);

        mensalidadeFormat = new CurrencyNumberAdapter(false);
        mensalidadeField.addKeyListener(mensalidadeFormat);
        mensalidadeField.addFocusListener(mensalidadeFormat);

        franquiaFormat = new CurrencyNumberAdapter(false);
        franquiaField.addKeyListener(franquiaFormat);
        franquiaField.addFocusListener(franquiaFormat);
    }

    private void calcularCotas() {
        try {
            Number number = valorFormat.getNumberFormat().parseNumber(valorField.getText());
            Double valor = Double.parseDouble(number.toString());
            CalculoCotas calculoCotas = new CalculoCotas(valor);
            Double cotas = calculoCotas.calculaCotas();
            cotasField.setText(df.format(cotas));
            mensalidadeField.setText(mensalidadeFormat.getNumberFormat().formatNumber(calculoCotas.calculaMensalidade()));
            franquiaField.setText(franquiaFormat.getNumberFormat().formatNumber(calculoCotas.calculaFranquia()));
        } catch (ParseException ex) {
            cotasField.setText(null);
            mensalidadeField.setText(null);
            franquiaField.setText(null);
        }
    }
    
    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancelarButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }
}
